package com.hyh.log.aspect;

import com.hyh.log.annotation.HyhLog;
import com.hyh.log.service.LogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

import javax.annotation.Resource;

/**
 * Log Aspect
 *
 * @Author: heyuhua
 * @Date: 2021/1/28 16:02
 */
@Aspect
public class LogAspect {


    /**
     * 日志服务
     */
    @Resource
    private LogService logService;

    /**
     * 环绕操作
     *
     * @param point
     * @param hyhLog
     * @return
     * @throws Throwable
     */
    @Around("@annotation(hyhLog)")
    public Object around(ProceedingJoinPoint point, HyhLog hyhLog) throws Throwable {
        String className = point.getTarget().getClass().getName();
        String methodName = point.getSignature().getName();
        logService.info("【类】:{},【方法】:{}", className, methodName);
        Object obj = point.proceed();
        // do something
        return obj;
    }

    /**
     * 前置操作
     *
     * @param point
     * @param hyhLog
     * @return
     * @throws Throwable
     */
    @Before("@annotation(hyhLog)")
    public void before(JoinPoint point, HyhLog hyhLog) throws Throwable {
        logService.info("执行前置操作..." + hyhLog.value());
        // do something
    }

    /**
     * 后置操作
     *
     * @param point
     * @param hyhLog
     * @return
     * @throws Throwable
     */
    @After("@annotation(hyhLog)")
    public void after(JoinPoint point, HyhLog hyhLog) throws Throwable {
        logService.info("执行后置操作..." + hyhLog.value());
        // do something
    }
}
